package com.xinshujia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinshujia.dao.DeviceDao;
import com.xinshujia.dto.DeviceDescDTO;
import com.xinshujia.entity.DeviceEntity;
import com.xinshujia.service.AccelerationStatisticsService;
import com.xinshujia.service.DeviceService;
import com.xinshujia.utils.StatusConstants;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-07-19 17:36       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 * 设备服务实现类
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceDao, DeviceEntity> implements DeviceService {
    private final DeviceDao deviceDao;
    private final AccelerationStatisticsService accelerationStatisticsService;

    public DeviceServiceImpl(DeviceDao deviceDao, AccelerationStatisticsService accelerationStatisticsService) {
        this.deviceDao = deviceDao;
        this.accelerationStatisticsService = accelerationStatisticsService;
    }
//    private final ThresholdService thresholdService;
//    private final AlarmRecordService alarmRecordService;
//    private final QuartzService quartzService;


    /**
     * 分页查询
     */
    @Override
    public Page<DeviceEntity> findAllByPage(Long pageNum, Long pageSize, String companyName) {
        QueryWrapper<DeviceEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(companyName)) {
            queryWrapper.like("company_name", companyName)
                    .or()
                    .like("device_guid", companyName);
        }
        Page<DeviceEntity> page = new Page<>(pageNum, pageSize);
        Page<DeviceEntity> deviceEntityPage = deviceDao.selectPage(page, queryWrapper);
        List<DeviceEntity> deviceEntities = deviceEntityPage.getRecords();

        // 在线设备的DeviceGuid集合
        List<String> deviceGuids = accelerationStatisticsService.selectOnlineDeviceGuids();

        List<DeviceEntity> deviceEntityList = new ArrayList<>();
        for (DeviceEntity deviceEntity : deviceEntities) {
            if (deviceGuids.contains(deviceEntity.getDeviceGuid())) {
                deviceEntity.setDeviceOnlineStatus(StatusConstants.DEVICE_ON_LINE);
            } else {
                deviceEntity.setDeviceOnlineStatus(StatusConstants.DEVICE_OFF_LINE);
            }
            deviceEntityList.add(deviceEntity);
        }
        deviceEntityPage.setRecords(deviceEntityList);
        return deviceDao.selectPage(page, queryWrapper);
    }

    /**
     * 添加设备
     */
    @Override
    public boolean insertDevice(DeviceEntity deviceEntity) {
        // 设备在线状态默认为离线
        deviceEntity.setDeviceOnlineStatus(StatusConstants.DEVICE_OFF_LINE);
        // 设备运行状态默认为停止
        deviceEntity.setDeviceRunStatus(StatusConstants.DEVICE_STOP);
        //
        deviceEntity.setAlarmStatus(StatusConstants.ALARM_CLOSE);
        // todo 只有设备在线的情况下才能判断机床是否运行（也是根据振动值）
        deviceEntity.setMachineStatus(StatusConstants.MACHINE_STOP);
        return deviceDao.insert(deviceEntity) > 0;
    }

    /**
     * 根据设备GUID查询设备
     */
    @Override
    public DeviceEntity findDeviceByDeviceGuid(String deviceGuid) {
        return deviceDao.findDeviceByDeviceGuid(deviceGuid);
    }

    /**
     * 根据Id更新设备
     */
    @Override
    public boolean updateDeviceById(DeviceEntity deviceEntity) {
        return deviceDao.updateById(deviceEntity) > 0;
    }

    /**
     * 根据DeviceId查询设备详情
     */
    @Override
    public DeviceDescDTO getDeviceDescByDeviceGuid(String deviceGuid) {
        // 设备
        DeviceDescDTO deviceDescDTO = new DeviceDescDTO();
        DeviceEntity deviceEntity = deviceDao.findDeviceByDeviceGuid(deviceGuid);

//        // 阈值
//        ThresholdEntity thresholdEntity = thresholdService.findThresholdByDeviceGuid(deviceGuid);
//
//        // 预警
//        AlarmRecordDTO alarmRecordDTO = alarmRecordService.getAlarmRecordsByDeviceGuid(deviceGuid).get(0);
//
//        // 定时任务
//        QuartzEntity quartzEntity = quartzService.findQuartzByDeviceGuid(deviceGuid);

        deviceDescDTO.setDeviceEntity(deviceEntity);
//        deviceDescDTO.setThresholdEntity(thresholdEntity);
//        deviceDescDTO.setAlarmRecordDTO(alarmRecordDTO);
//        deviceDescDTO.setQuartzEntity(quartzEntity);

        return deviceDescDTO;
    }

    /**
     * 更新设备预警状态
     */
    @Override
    public void updateDeviceAlarmStatus(String deviceGuid, int operateType) {
        deviceDao.updateDeviceAlarmStatus(deviceGuid, operateType);
    }
}
